package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

public class day02 {
    public static void main(String[] args) {
        ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
        String driver = bundle.getString("driver");
        try {
            //Class.forName("com.mysql.cj.jbbc.Drivar")
            //类加载的时候，静态代码会执行
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            System.out.println("找不到 MySQL 驱动类: " + e.getMessage());
            return;
        }

        String url = bundle.getString("url");
        String username = bundle.getString("name");
        String password = bundle.getString("password");

        try (Connection conn = DriverManager.getConnection(url, username, password);
             Statement statement = conn.createStatement()) {

            // 执行 SQL 语句
            String sql = "update dept set dname = '销售部', loc = '上海' where deptno = 20";
            //String sql = "delete from dept where deptno = 50";
            //String sql = "insert into dept(deptno, dname, loc) values(60, '人事部', '北京')";
            // statement.executeUpdate(sql) 专门执行增删改的语句
            // 返回值是影响数据库中的条数
            int count = statement.executeUpdate(sql);
            System.out.println("影响的行数: " + count);

        } catch (SQLException e) {
            System.out.println("数据库操作出错: " + e.getMessage());
        }
    }
}
